package com.woniu.service.impl;

import com.woniu.dao.AdminMapper;
import com.woniu.dao.RolesMapper;
import com.woniu.dao.RolesMenusMapper;
import com.woniu.exception.RoleServiceException;
import com.woniu.pojo.Admin;
import com.woniu.pojo.Roles;
import com.woniu.pojo.RolesMenusKey;
import com.woniu.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class RolesServiceImpl implements RolesService {
    @Autowired
    private RolesMapper rolesMapper;

    @Autowired
    private RolesMenusMapper rolesMenusMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Roles> findAll() {
        return rolesMapper.selectAll();
    }

    @Override
    public List<Roles> findByPage(Integer currentPage, Integer pageSize) {
        return rolesMapper.selectByPage(currentPage, pageSize);
    }

    @Override
    public Roles findByRoleName(String roleName) {
        return rolesMapper.selectRolesByName(roleName);
    }

    @Override
    public void saveRole(Roles roles, List<Integer> keys) {
        System.out.println("添加之前角色Id："+roles.getRoleId());
        //添加角色,同时返回新添加角色这条数据的id
        rolesMapper.saveRole(roles);
        System.out.println("添加后的角色Id："+roles.getRoleId());
        //授权：添加权限信息
        List<RolesMenusKey> list = new ArrayList();
        for (Integer menuId : keys) {
            RolesMenusKey rolesMenusKey = new RolesMenusKey();
            rolesMenusKey.setMenuId(menuId);
            rolesMenusKey.setRoleId(roles.getRoleId());
            list.add(rolesMenusKey);
        }
        //批量添加
        rolesMenusMapper.saveRolesMenus(list);
    }

    @Override
    public Roles findById(Integer id) {
        return rolesMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateRole(Roles roles, List<Integer> menuIds) {
        rolesMapper.updateByPrimaryKeySelective(roles);
        //将默认拥有的权限删除
        rolesMenusMapper.deleteByRoleId(roles.getRoleId());
        //添加
        List<RolesMenusKey> list = new ArrayList();
        for (Integer menuId : menuIds) {
            RolesMenusKey rolesMenusKey = new RolesMenusKey();
            rolesMenusKey.setRoleId(roles.getRoleId());//当前角色对象
            rolesMenusKey.setMenuId(menuId);//可以访问哪些菜单(资源)
            list.add(rolesMenusKey);
        }
        rolesMenusMapper.saveRolesMenus(list);//批量添加

    }


    @Override
    public boolean checkRoleName(Roles roles) {
        Roles dbRoles = rolesMapper.selectRolesByName(roles.getRoleName());
        if(dbRoles == null){
            return false;
        }
        if(dbRoles.getRoleId().equals(roles.getRoleId())){
            return false;
        }
        return true;
    }

    @Override
    public void delRole(Integer roleId) {
        List<Admin> admins = adminMapper.selectByRoleId(roleId);
        if(admins.size()>0){
            throw new RoleServiceException("该角色已经被分配，不能删除");
        }
        System.out.println("角色ID:"+roleId);
        //删除角色的同时要删除中间表中存储的数据（已经被赋予的权限）
        rolesMenusMapper.deleteByRoleId(roleId);

        rolesMapper.deleteByPrimaryKey(roleId);
    }

    @Override
    public void batchDel(List<Integer> rolesId) {
        for(Integer os:rolesId){
            System.out.println(os+",");
        }
        List<Admin> admins = adminMapper.selectAdminByRoleId(rolesId);
        if(admins.size()>0){
            Roles roles = rolesMapper.selectByPrimaryKey(admins.get(0).getRoleId());
            throw new RoleServiceException(roles.getRoleName()+"角色已经被分配给用户，不能删除");
        }
        rolesMapper.batchDel(rolesId);
        //删除中间表中的数据
        rolesMenusMapper.batchDeleteByRoleId(rolesId);
    }
}
